import config from "@/config"
import {
	getToken
} from "./auth"
/**
* 显示消息提示框
* @param content 提示的标题
*/
export function toast(content) {
  uni.showToast({
    icon: 'none',
    title: content
  })
}

export function uploadFilePromise(url) {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: config.baseUrl + '/common/upload', // 仅为示例，非真实的接口地址
			filePath: url,
			name: 'file',
			header: {
				'content-type': 'multipart/form-data',
				'Authorization': getToken(),
			},
			success: (resp) => {
				resolve(resp.data);
			},
		})
	})
}

/**
* 显示模态弹窗
* @param content 提示的标题
*/
export function showConfirm(content,title) {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: title || '提示', 
      content: content,
      cancelText: '取消',
      confirmText: '确定',
      success: function(res) {
        resolve(res)
      }
    })
  })
}

/**
* 参数处理
* @param params 参数
*/
export function tansParams(params) {
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName]
    var part = encodeURIComponent(propName) + "="
    if (value !== null && value !== "" && typeof (value) !== "undefined") {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
            let params = propName + '[' + key + ']'
            var subPart = encodeURIComponent(params) + "="
            result += subPart + encodeURIComponent(value[key]) + "&"
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&"
      }
    }
  }
  return result
}

/**
 * 判断是否存在上一个页面
 */
export function hasPreviousPage() {
	const pages = getCurrentPages();
	return pages.length > 1;
}

export function navbarPage() {
	if (hasPreviousPage()) {
		uni.navigateBack()
		return
	}
	uni.switchTab({
		url: '/pages/index'
	})
}


/**
 * @param {Object} obj
 * 判断对象是否为空
 */
export function isEmptyObject(obj) {
	return Object.keys(obj).length === 0 && obj.constructor === Object;
}

/**
 * 手机号隐藏中键四位
 * @param {Object} params
 */
export function starPhone(phone) {
	if (!phone) {
		return
	}
	return phone.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2')
}